package org.ruoyi.aibox.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.ruoyi.common.idempotent.annotation.RepeatSubmit;
import org.ruoyi.common.log.annotation.Log;
import org.ruoyi.common.web.core.BaseController;
import org.ruoyi.core.page.PageQuery;
import org.ruoyi.common.core.domain.R;
import org.ruoyi.common.core.validate.AddGroup;
import org.ruoyi.common.core.validate.EditGroup;
import org.ruoyi.common.log.enums.BusinessType;
import org.ruoyi.common.excel.utils.ExcelUtil;
import org.ruoyi.aibox.domain.vo.DocAuditLogVo;
import org.ruoyi.aibox.domain.bo.DocAuditLogBo;
import org.ruoyi.aibox.service.IDocAuditLogService;
import org.ruoyi.core.page.TableDataInfo;

/**
 * 文档审核日志
 *
 * @author xpeter
 * @date 2025-04-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/aibox/auditLog")
public class DocAuditLogController extends BaseController {

    private final IDocAuditLogService docAuditLogService;

    /**
     * 查询文档审核日志列表
     */
    @SaCheckPermission("aibox:auditLog:list")
    @GetMapping("/list")
    public TableDataInfo<DocAuditLogVo> list(DocAuditLogBo bo, PageQuery pageQuery) {
        return docAuditLogService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出文档审核日志列表
     */
    @SaCheckPermission("aibox:auditLog:export")
    @Log(title = "文档审核日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DocAuditLogBo bo, HttpServletResponse response) {
        List<DocAuditLogVo> list = docAuditLogService.queryList(bo);
        ExcelUtil.exportExcel(list, "文档审核日志", DocAuditLogVo.class, response);
    }

    /**
     * 获取文档审核日志详细信息
     *
     * @param logId 主键
     */
    @SaCheckPermission("aibox:auditLog:query")
    @GetMapping("/{logId}")
    public R<DocAuditLogVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long logId) {
        return R.ok(docAuditLogService.queryById(logId));
    }

    /**
     * 新增文档审核日志
     */
    @SaCheckPermission("aibox:auditLog:add")
    @Log(title = "文档审核日志", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DocAuditLogBo bo) {
        return toAjax(docAuditLogService.insertByBo(bo));
    }

    /**
     * 修改文档审核日志
     */
    @SaCheckPermission("aibox:auditLog:edit")
    @Log(title = "文档审核日志", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DocAuditLogBo bo) {
        return toAjax(docAuditLogService.updateByBo(bo));
    }

    /**
     * 删除文档审核日志
     *
     * @param logIds 主键串
     */
    @SaCheckPermission("aibox:auditLog:remove")
    @Log(title = "文档审核日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{logIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] logIds) {
        return toAjax(docAuditLogService.deleteWithValidByIds(List.of(logIds), true));
    }
}
